package me.opbukkitdemo.fabric.mixin;

import me.opbukkitdemo.common.event.PlayerOpChangeEvent;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_21_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

/**
 * @author langle__
 * @version 1.0
 */
@Mixin(value = CraftPlayer.class, remap = false)
public abstract class CraftPlayerMixin {

    @Inject(method = "setOp", at = @At("HEAD"), cancellable = true)
    private void setOp(boolean value, CallbackInfo ci) {
        Player player = ((CraftPlayer) (Object) this);
        PlayerOpChangeEvent event = new PlayerOpChangeEvent(player, player.isOp(), value);
        Bukkit.getPluginManager().callEvent(event);
        if (event.isCancelled()) {
            ci.cancel();
        }
    }

}
